package be.sc.classicnetwork.model;

import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.ManyToMany;

import org.apache.commons.lang.builder.EqualsBuilder;

/**
 * Role.
 */
@Entity
public class Role extends AbstractEntity {

    public static final Role ANONYMOUS = new Role("ROLE_ANONYMOUS");

    public static final Role SUB_ADMIN = new Role("ROLE_SUB_ADMIN");

    public static final Role ADMIN = new Role("ROLE_ADMIN");

    public static final Role SUPER_ADMIN = new Role("ROLE_SUPER_ADMIN");

    private static final long serialVersionUID = -7614990373991732186L;

    private String rolename;

    private String description;

    @ManyToMany(cascade = { CascadeType.ALL }, mappedBy = "roles")
    private Set<User> users;

    /**
     * Creates a default Role.
     */
    public Role() {
    }

    /**
     * Creates a Role witht the specified rolename.
     * 
     * @param rolename
     */
    public Role(String rolename) {
	this.rolename = rolename;
    }

    /**
     * @return true if both rolename attribute are equals
     */
    @Override
    public boolean equals(Object anObject) {
	if (anObject instanceof Role == false) {
	    return false;
	}
	if (this == anObject) {
	    return true;
	}
	Role role = (Role) anObject;
	return new EqualsBuilder().append(rolename, role.getRolename()).isEquals();
    }

    /**
     * Adds the specified User to this Role.
     * 
     * @param user
     */
    public void addUser(User user) {
	if (users == null) {
	    users = new HashSet<User>();
	}
	users.add(user);
    }

    public String getRolename() {
	return rolename;
    }

    public void setRolename(String rolename) {
	this.rolename = rolename;
    }

    public String getDescription() {
	return description;
    }

    public void setDescription(String description) {
	this.description = description;
    }

    public Set<User> getUsers() {
	return users;
    }

    public void setUsers(Set<User> users) {
	this.users = users;
    }

}
